package webmoney.cryptography.examination;

import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import webmoney.cryptography.Signer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static org.junit.Assert.assertEquals;

public class SignerTest {

    static Signer signer;

    @BeforeClass
    public static void signerInitialization() throws IOException {

        byte[] exponent = new byte[]{(byte) 0x7D, (byte) 0xB2, (byte) 0x36, (byte) 0x08, (byte) 0xBD, (byte) 0x9E, (byte) 0x8C, (byte) 0x0F, (byte) 0xA8, (byte) 0xED, (byte) 0xAE, (byte) 0x02, (byte) 0x6C, (byte) 0x29, (byte) 0x59, (byte) 0x1B, (byte) 0x22, (byte) 0xB0, (byte) 0xCF, (byte) 0x37, (byte) 0xC2, (byte) 0xFE, (byte) 0xA7, (byte) 0x13, (byte) 0xB1, (byte) 0x29, (byte) 0xBF, (byte) 0x0B, (byte) 0x88, (byte) 0x57, (byte) 0xC0, (byte) 0x59, (byte) 0x1B, (byte) 0xBD, (byte) 0xA4, (byte) 0xE9, (byte) 0xB3, (byte) 0x1D, (byte) 0x2E, (byte) 0xCC, (byte) 0x5F, (byte) 0xA0, (byte) 0x8F, (byte) 0xE0, (byte) 0x9D, (byte) 0xE6, (byte) 0xA1, (byte) 0xE5, (byte) 0x0C, (byte) 0x6C, (byte) 0xEE, (byte) 0x9B, (byte) 0xD2, (byte) 0x67, (byte) 0xE3, (byte) 0x90, (byte) 0x10, (byte) 0xCE, (byte) 0xBD, (byte) 0x58, (byte) 0xFE, (byte) 0xAA, (byte) 0x28, (byte) 0xBB, (byte) 0xBF, (byte) 0x00};
        byte[] modulus = new byte[]{(byte) 0x55, (byte) 0x1F, (byte) 0xFF, (byte) 0x9E, (byte) 0xC2, (byte) 0xFE, (byte) 0xF3, (byte) 0x4C, (byte) 0xD0, (byte) 0x02, (byte) 0x67, (byte) 0xBE, (byte) 0xB9, (byte) 0xD7, (byte) 0xCF, (byte) 0x6A, (byte) 0x72, (byte) 0x63, (byte) 0x66, (byte) 0xE4, (byte) 0xC9, (byte) 0x5D, (byte) 0xB5, (byte) 0x62, (byte) 0x77, (byte) 0x44, (byte) 0x3C, (byte) 0x42, (byte) 0x7B, (byte) 0x65, (byte) 0x9D, (byte) 0xF0, (byte) 0x1E, (byte) 0x76, (byte) 0xBE, (byte) 0x7B, (byte) 0x4D, (byte) 0x99, (byte) 0xC5, (byte) 0x8C, (byte) 0xA4, (byte) 0xA6, (byte) 0x09, (byte) 0x62, (byte) 0x6C, (byte) 0xA4, (byte) 0x91, (byte) 0x94, (byte) 0xEE, (byte) 0xC2, (byte) 0xEC, (byte) 0xDD, (byte) 0x53, (byte) 0xF5, (byte) 0x73, (byte) 0x46, (byte) 0xEC, (byte) 0xD6, (byte) 0xE1, (byte) 0xB0, (byte) 0xD4, (byte) 0xCB, (byte) 0xD6, (byte) 0xF0, (byte) 0x6E, (byte) 0x05};

        signer = new Signer(exponent, modulus);
    }

    @Test
    public void basic() throws Exception {
        assertEquals(
                "ba4fb77e2cb230f3746f5844df4d4bfb8b939c5a39a55cd5c4cdd0c8371cb0c564ca079f96602065aab8b784e1581db5df0a3b5cef19dda9b1827577de18077e02eb",
                signer.sign("test", false));
    }

    @Test
    public void longString() throws UnsupportedEncodingException {
        assertEquals(
                "f9c6145f8e5c79a412dbc330c872e59990b55105d0ecf97f556c662c3b878a464f81067e82174ac21a37f1bd54f0618f2fcc6f4c7a355605bc6ad3bafb01f7e20126",
                signer.sign("LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString" +
                        "LongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongStringLongString", false));
    }

    @Test
    public void blank() throws UnsupportedEncodingException {
        assertEquals(
                "d5295349168cdb6fdbdb9c1984836cc1db939dda1dd6e18112db007154db8065b25583c71bd952f88f24f2515fbc4bb3037e5cedd5ef33cd336960b6a655537304bf",
                signer.sign("                                                                  ", false));
    }

    @Test
    public void symbols() throws UnsupportedEncodingException {
        assertEquals(
                "61b8260b9d5d7cce8e1c0262f0722b8114a7b3e233d76f4b93d08933a89d23d73acfb2d960966b0c175a8604e7c71c391b7f2a64743b3c3b11c5d07ed224445300b3",
                signer.sign("!@#$%^&*()-=_{}[]\"'1234567890.,%:?<>/\\", false));
    }

    @Test
    public void specSymbols() throws UnsupportedEncodingException {
        assertEquals(
                "c37c96e02f8678a13a43a3bade0ba77275da0d9869d18856daaf4b413106a9bfea74054462d851a0c5a2596dbf9c371284c61b3741b35343c91d2978db328c2f00c9",
                signer.sign("\n\n\n\n\t\t\t\t\n\n\n\n", false));
    }

    @Test
    public void cyrillic1251() throws UnsupportedEncodingException {
        Assert.assertEquals(
                "49a133e33418fe2c90d91332dd8e8e9c4fe9b09792b8e7c164ab7e3ce4a949c5855e3ed96ab62f40c6d4c7abad295f7ae01a6a58d4c984b1d44740d1000e6ce203b9",
                signer.sign("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдезжийклмнопрстуфхцчшщъыьэюя", false));
    }

    @Test
    public void mixed() throws UnsupportedEncodingException {
        Assert.assertEquals(
                "79848351bebd6476720ae9837abaad5fbf4bb82235956179369b664aec3f097b0a513b8497371f164e5e492a884aa34283f23b0cefba3edc189745ab6bb35e78032f",
                signer.sign("\n&&&<Текст> !@#$%^ &*()-=_{АБВГД Е Ё Ж    З И Й К Л    М НО ПРСТУ   Ф ХЦЧШЩЪЫЬЭЮЯабвгдеёзжийклмнопрстуфхцчшщъыьэюя}[]\"'1234567890.,%:?<><b>Привет мир!</b></testing>", false));
    }
}